home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / g__~1 / gplibs20.zoo / igetline.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-13  |  3.9 KB  |  136 lines

  1. //    This is part of the iostream library, providing input/output for C++.
  2. //    Copyright (C) 1992 Per Bothner.
  3. //
  4. //    This library is free software; you can redistribute it and/or
  5. //    modify it under the terms of the GNU Library General Public
  6. //    License as published by the Free Software Foundation; either
  7. //    version 2 of the License, or (at your option) any later version.
  8. //
  9. //    This library is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. //    Library General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU Library General Public
  15. //    License along with this library; if not, write to the Free
  16. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include <iostream.h>
  19. #include <string.h>
  20.  
  21. istream& istream::getline(char* buf, _G_size_t len, char delim)
  22. {
  23.     _gcount = 0;
  24.     if (ipfx1()) {
  25.     streambuf *sb = rdbuf();
  26.     long count = sb->sgetline(buf, len, delim, -1);
  27.     if (count == len-1)
  28.         set(ios::failbit);
  29.     else {
  30.         int ch = sb->sbumpc();
  31.         if (ch == EOF)
  32.         set(ios::failbit|ios::eofbit);
  33.         else if (ch == (unsigned char)delim)
  34.         count++;
  35.         else
  36.         sb->sungetc(); // Leave delimiter unread.
  37.     }
  38.     _gcount = count;
  39.     }
  40.     return *this;
  41. }
  42.  
  43. istream& istream::get(char* buf, _G_size_t len, char delim)
  44. {
  45.     _gcount = 0;
  46.     if (ipfx1()) {
  47.     streambuf *sbuf = rdbuf();
  48.     long count = sbuf->sgetline(buf, len, delim, -1);
  49.     if (count < 0 || (count == 0 && sbuf->sgetc() == EOF))
  50.         set(ios::failbit|ios::eofbit);
  51.     else
  52.         _gcount = count;
  53.     }
  54.     return *this;
  55. }
  56.  
  57. //    This is part of the iostream library, providing input/output for C++.
  58. //    Copyright (C) 1992 Free Software Foundation.
  59. //
  60. //    This library is free software; you can redistribute it and/or
  61. //    modify it under the terms of the GNU Library General Public
  62. //    License as published by the Free Software Foundation; either
  63. //    version 2 of the License, or (at your option) any later version.
  64. //
  65. //    This library is distributed in the hope that it will be useful,
  66. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  67. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  68. //    Library General Public License for more details.
  69. //
  70. //    You should have received a copy of the GNU Library General Public
  71. //    License along with this library; if not, write to the Free
  72. //    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  73.  
  74. // from Doug Schmidt
  75.  
  76. #define CHUNK_SIZE 512
  77.  
  78. /* Reads an arbitrarily long input line terminated by a user-specified
  79.    TERMINATOR.  Super-nifty trick using recursion avoids unnecessary calls
  80.    to NEW! */
  81.  
  82. char *_sb_readline (streambuf *sb, long& total, char terminator) 
  83. {
  84.     char buf[CHUNK_SIZE+1];
  85.     char *ptr;
  86.     int ch;
  87.     
  88.     long count = sb->sgetline(buf, CHUNK_SIZE+1, terminator, -1);
  89.     if (count == EOF)
  90.     return NULL;
  91.     ch = sb->sbumpc();
  92.     long old_total = total;
  93.     total += count;
  94.     if (ch != EOF && ch != terminator) {
  95.     total++; // Include ch in total.
  96.     ptr = _sb_readline(sb, total, terminator);
  97.     if (ptr) {
  98.         memcpy(ptr + old_total, buf, count);
  99.         ptr[old_total+count] = ch;
  100.     }
  101.     return ptr;
  102.     }
  103.     
  104.     if (ptr = new char[total+1]) {
  105.     ptr[total] = '\0';
  106.     memcpy(ptr + total - count, buf, count);
  107.     return ptr;
  108.     } 
  109.     else 
  110.     return NULL;
  111. }
  112.  
  113. /* Reads an arbitrarily long input line terminated by TERMINATOR.
  114.    This routine allocates its own memory, so the user should
  115.    only supply the address of a (char *). */
  116.  
  117. istream& istream::gets(char **s, char delim /* = '\n' */)
  118. {
  119.     if (ipfx1()) {
  120.     long size = 0;
  121.     streambuf *sb = rdbuf();
  122.     *s = _sb_readline (sb, size, delim);
  123.     _gcount = *s ? size : 0;
  124.     if (sb->_flags & _S_EOF_SEEN) {
  125.         set(ios::eofbit);
  126.         if (_gcount == 0)
  127.         set(ios::failbit);
  128.     }
  129.     }
  130.     else {
  131.     _gcount = 0;
  132.     *s = NULL;
  133.     }
  134.     return *this;
  135. }
  136.